package com.kkd.kkdapp.pinnedsection;

import java.io.IOException;

import pl.droidsonroids.gif.GifDrawable;
import android.content.Context;
import android.content.res.Resources.NotFoundException;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.kkd.kkdapp.R;

/**
 * ����ˢ�µ�headerview ����ֱ�Ӹ���ճ��ֱ��ʹ��,�����޸������
 * 
 * @author max.chengdu 2015��11��29��
 * 
 */
public class RefreshHeader extends LinearLayout {
	// ԭʼ״̬
	public final static int STATE_NORMAL = 0;
	// ��������
	public final static int STATE_READY = 1;
	// ����ˢ��
	public final static int STATE_REFRESHING = 2;

	// refresh_footer.xml����Ŀؼ�
	private LinearLayout mContainer;
	private ImageView mArrowImageView;
	private ImageView mProgressBar;
	private TextView mHintTextView;

	/**
	 * ��ǰͷ����״̬
	 * 
	 * @param context
	 */
	private int mState = STATE_NORMAL;
	// ���ض���
	private Animation mRotateAnim;
	private Animation mRotateDownAnim;

	private final int ROATE_ANIM_DURATION = 180;

	public RefreshHeader(Context context) {
		super(context);
		initView(context);
	}

	/**
	 * ��ʼ��
	 * 
	 * @param context
	 */
	private void initView(Context context) {
		// 初始化情况,下拉刷新的高度为0
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, 0);
		mContainer = (LinearLayout) LinearLayout.inflate(context,
				R.layout.refresh_header, null);
		// 为linearlayout添加子view
		addView(mContainer, lp);
		setGravity(Gravity.BOTTOM);
		// 初始化控件
		mArrowImageView = (ImageView) findViewById(R.id.refresh_header_arrow);
		mProgressBar = (ImageView) findViewById(R.id.refresh_header_progressbar);
		mHintTextView = (TextView) findViewById(R.id.refresh_header_hint_textview);

		GifDrawable drawable;
		try {
			drawable = new GifDrawable(context.getResources(),
					R.drawable.four_dot);

			mProgressBar.setImageDrawable(drawable);
		} catch (NotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 旋转动画,顺时针旋转180度
		mRotateAnim = new RotateAnimation(0.0f, -180.0f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		mRotateAnim.setDuration(ROATE_ANIM_DURATION);
		mRotateAnim.setFillAfter(true);

		// 到底部的旋转动画
		mRotateDownAnim = new RotateAnimation(-180.0f, 0.0f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		mRotateDownAnim.setDuration(ROATE_ANIM_DURATION);
		mRotateAnim.setFillAfter(true);
	}

	public void setState(int state) {
		if (state == mState) {
			return;
		}
		// 正在刷新的时候,显示进度条
		if (state == STATE_REFRESHING) {
			// // 清除动画
			// mArrowImageView.clearAnimation();
			// 箭头消失
			mArrowImageView.setVisibility(View.INVISIBLE);
			mProgressBar.setVisibility(View.VISIBLE);
		} else {
			// 进度条消失,显示箭头
			mArrowImageView.setVisibility(View.VISIBLE);
			mProgressBar.setVisibility(View.INVISIBLE);
		}

		switch (state) {
		// 原始状态下
		case STATE_NORMAL:
			// if (mState == STATE_READY) {
			// // 箭头向下
			// mArrowImageView.startAnimation(mRotateDownAnim);
			// }
			// if (mState == STATE_REFRESHING) {
			// mArrowImageView.clearAnimation();
			// }
			mHintTextView.setText("下拉刷新");
			break;
		// 正在下拉
		case STATE_READY:
			if (mState != STATE_READY) {
				// mArrowImageView.clearAnimation();
				// // 箭头向上
				// mArrowImageView.setAnimation(mRotateAnim);
				mHintTextView.setText("松开刷新");
			}
			break;
		case STATE_REFRESHING:
			mHintTextView.setText("正在刷新");
			break;

		default:
			break;
		}
		mState = state;
	}

	// �趨���Ӹ߶�
	public void setVisiableHeight(int height) {
		if (height < 0) {
			height = 0;
		}
		// ��ȡ��ǰ�ĸ߶�
		LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mContainer
				.getLayoutParams();
		lp.height = height;
		mContainer.setLayoutParams(lp);
	}

	// ��ÿ��Ӹ߶�
	public int getVisiableHeight() {
		return mContainer.getHeight();
	}
}
